package com.cadre.common.utils.oldutil;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/* loaded from: Md5EncodeUtil.class */
public class Md5EncodeUtil {
    public static String md5Encode(String str) {
        return md5Encode(str, "UTF-8", true);
    }

    public static String md5EncodeForDotNet(String str) {
        return md5Encode(str, "UTF-16LE", false);
    }

    public static String md5Encode(String str, String charsetName, boolean with0) {
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest sha = MessageDigest.getInstance("MD5");
            sha.update(str.getBytes(charsetName));
            for (byte b : sha.digest()) {
                int iInt = b & 255;
                if (with0) {
                    sb.append(Integer.toString(iInt + 256, 16).substring(1));
                } else {
                    sb.append(Integer.toString(iInt, 16));
                }
            }
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchAlgorithmException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public static String GetBase64Str(byte[] bs) {
        return Base64.getEncoder().encodeToString(bs);
    }

    public static String md5EncodeNew(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        new BASE64Encoder();
        byte[] data = md5.digest(str.getBytes("UTF-8"));
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            sBuffer.append(String.format("%02x", Byte.valueOf(data[i])));
        }
        return sBuffer.toString();
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        System.out.println("字符串140402611007121的各种md5加密结果：");
        System.out.println("Java标准加密\t" + md5Encode("140402611007121"));
        System.out.println("Java对接C#加密\t" + md5EncodeForDotNet("140402611007121"));
        try {
            System.out.println("Java对接C#加密BASE编码\t" + GetBase64Str(md5EncodeForDotNet("140402611007121").getBytes("utf-8")));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        System.out.println("Java对接C#二次加密\t" + md5EncodeForDotNet(md5EncodeForDotNet("140402611007121")));
        try {
            System.out.println("Java对接C#二次加密BASE编码\t" + GetBase64Str(md5EncodeForDotNet(md5EncodeForDotNet("140402611007121")).getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("=========>新方法======>");
        System.out.println("Java新方法MD5加密\t" + md5EncodeNew("140402611007121"));
        System.out.println("Java新方法MD5加密BASE64\t" + GetBase64Str(md5EncodeNew("140402611007121").getBytes()));
        System.out.println("Java新方法二次MD5加密\t" + md5EncodeNew(md5EncodeNew("140402611007121")));
        new DesUtilNet();
        try {
            System.out.println("Java新方法二次MD5加密DES及Base64\t" + DesUtilNet.GetBase64Str(DesUtilNet.encrypt(md5EncodeNew(md5EncodeNew("140402611007121")))));
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        System.out.println("Java新方法二次MD5加密BASE64\t" + GetBase64Str(md5EncodeNew(md5EncodeNew("140402611007121")).getBytes()));
    }
}
